package worker

import (
	"NovelBook/logic/account"
	"NovelBook/models/res"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Modify(c *gin.Context) {
	idAny, exist := c.Get(auth.WorkerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var userAvatar res.UserAvatar
	if err := c.BindJSON(&userAvatar); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	// 调用修改信息
	if err := account.Modify(id, userAvatar, account.WORKER); err != nil {
		response.Fail(c, http.StatusInternalServerError, response.CodeSystemBusy)
		return
	}

	response.Success(c, nil)
}

// ResetPassword 重置密码
// 重置密码之前需要先获取验证码
func ResetPassword(c *gin.Context) {
	var resetInfo res.ResetInfo
	if err := c.BindJSON(&resetInfo); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	// 调用重置密码
	if err := account.ResetPassword(resetInfo, account.WORKER); err != nil {
		response.Fail(c, http.StatusInternalServerError, response.CodeSystemBusy)
		return
	}

	response.Success(c, nil)
}
